//
//  UIBarButtonItem + Category.swift
//  StudySwift
//
//  Created by duan on 2018/1/19.
//  Copyright © 2018年 fan. All rights reserved.
//

import UIKit

extension UIBarButtonItem
{
    // 类方法
    class func crateBarButtonItem(imageName:String, target: AnyObject?, action: Selector) -> UIBarButtonItem
    {
        let btn = UIButton()
        btn.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
        btn.setImage(UIImage(named: imageName), for: UIControl.State.normal)
        btn.setImage(UIImage(named: imageName + "_highlighted"), for: UIControl.State.highlighted)
        btn.addTarget(target, action: action, for: UIControl.Event.touchUpInside)
        return UIBarButtonItem(customView: btn)
    }
    
    //‘便利’构造函数:1>convenience 2>在构造函数中必须明确调用一个设计的构造函数（self）
    convenience init (imageName:String,highImageName:String = "",size:CGSize = CGSize.zero){
        let btn = UIButton()
        btn.setImage(UIImage(named: imageName), for:UIControl.State.normal)
        if highImageName != "" {
            btn.setImage(UIImage(named: highImageName), for:UIControl.State.highlighted)
        }
        if size == CGSize.zero {
            btn.sizeToFit()
        } else {
            btn.frame = CGRect(origin: CGPoint.zero, size: size)
        }
        self.init(customView: btn)
    }
}
